<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="项目id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <a-input v-model="model.projectId" placeholder="请输入项目id"></a-input>
              {{projectId}}
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="标段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bidSectionCode">
              <a-select placeholder="请选择标段" v-model="model.bidSectionId">
                <a-select-option v-for="item in exSectionList" :key="item.id" :value="item.id">
                  {{ item.bidSectionName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="queType">
              <j-dict-select-tag
                type="list"
                v-model="model.queType"
                dictCode="questioning_type"
                placeholder="请选择问题类型"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="queContent">
              <a-textarea v-model="model.queContent" rows="4" placeholder="请输入问题描述 " />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item
              label="提问时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="queDate"
            >
              <j-date
                placeholder="请选择提问时间"
                v-model="model.queDate"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="提问人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="queName">
              <a-input v-model="model.queName" placeholder="请输入提问人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="问题状态 - 参考b.9.18问题状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="queStatus"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.queStatus"
                dictCode="questioning_status"
                placeholder="请选择问题状态 - 参考b.9.18问题状态"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题回复 - 自由文本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="answer">
              <a-textarea v-model="model.answer" rows="4" placeholder="请输入问题回复 - 自由文本" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachmentCode">
              <j-upload v-model="model.attachmentCode"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ExOnlineQuestioningForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    projectId: {
      default: '',
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        projectId: [{ required: true, message: '请输入项目id!' }],
        queType: [{ required: true, message: '请输入问题类型!' }],
        queContent: [{ required: true, message: '请输入问题描述 - 自由文本!' }],
        queDate: [{ required: true, message: '请输入提问时间 - 日期时间型，格式为yyyymmddhhmmss!' }],
        queName: [{ required: true, message: '请输入提问人!' }],
        queStatus: [{ required: true, message: '请输入问题状态 - 参考b.9.18问题状态!' }],
      },
      url: {
        add: '/exOnlineQuestioning/exOnlineQuestioning/add',
        edit: '/exOnlineQuestioning/exOnlineQuestioning/edit',
        queryById: '/exOnlineQuestioning/exOnlineQuestioning/queryById',
      },
      exSectionList: [], 
    }
  },
  watch: {
    projectId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.model.projectId = val
          this.getProjectSection()
        }
      },
    },
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    getProjectSection() {
      this.exSectionList = []
      getAction('/exProject/exSection/listAll', { projectId: this.projectId })
        .then((res) => {
          if (res.success) {
            var dataSource = res.result
            for (let i = 0; i < dataSource.length; i++) {
              this.exSectionList.push(dataSource[i])
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>